package com.ruoyi.web.controller.system;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.LmTaskTypeVO;
import com.ruoyi.common.core.domain.entity.LmSysUser;
import com.ruoyi.common.core.domain.entity.LmTaskType;
import com.ruoyi.system.mapper.LmTaskTypeMapper;
import com.ruoyi.system.service.ILmSysUserService;
import com.ruoyi.system.service.ILmTaskTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/taskType")
public class LmTaskTypeController extends BaseController {

    @Autowired
    private ILmTaskTypeService taskTypeService;


    @Autowired
    private ILmSysUserService lmSysUserService;

    /**
     * 查询任务类型列表（递归）
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(LmTaskType taskType) {
        LmSysUser sysUser = lmSysUserService.getById(StpUtil.getLoginIdAsLong());
        System.err.println("sysUser-----------"+sysUser);
        String taskTypeId = sysUser.getTaskTypeId();
        if (sysUser.getIdentity() == 1){
            taskTypeId = null;
        }
        List<LmTaskTypeVO> list = taskTypeService.selectTaskTypeList(taskType,taskTypeId);
        return AjaxResult.success(list);
    }

    @GetMapping("/pullDownList")
    @ResponseBody
    public AjaxResult pullDownList(LmTaskType taskType) {
        LmSysUser sysUser = lmSysUserService.getById(StpUtil.getLoginIdAsLong());
        String taskTypeId = sysUser.getTaskTypeId();
        if (sysUser.getIdentity() == 1){
            taskTypeId = null;
        }
        List<LmTaskType> list = taskTypeService.selectTaskTypeListNew(taskType,taskTypeId);
        return AjaxResult.success(list);
    }

    /**
     * 获取任务类型详细信息
     */
    @GetMapping(value = "/detail")
    public AjaxResult getInfo(Integer id) {
        return AjaxResult.success(taskTypeService.selectTaskTypeById(id));
    }

    /**
     * 新增任务类型
     */
    @PostMapping("add")
    public AjaxResult add(LmTaskType taskType) {
        taskType.setCreateTime(new Date());
        taskType.setDelFlag(0);
        return toAjax(taskTypeService.insertTaskType(taskType));
    }

    /**
     * 修改任务类型
     */
    @PostMapping("edit")
    public AjaxResult edit(LmTaskType taskType) {
        taskType.setUpdateTime(new Date());
        return toAjax(taskTypeService.updateTaskType(taskType));
    }

    /**
     * 删除任务类型
     */
    @GetMapping("/delete")
    public AjaxResult remove(Integer id) {
        return toAjax(taskTypeService.deleteTaskTypeById(id));
    }



    @Autowired
    private LmTaskTypeMapper taskTypeMapper;


    /**
     * 根据所选的任务类型，查询遥测数据
     * @param taskTypeIds
     * @return
     */
    @GetMapping("getTelemetryListByTaskTypeId")
    public AjaxResult getTelemetryListByTaskTypeId(String taskTypeIds){
        List<Integer> idlist=convertToIntegerList(taskTypeIds);
        List<Map<String, Object>> list = taskTypeMapper.getTelemetryListByTaskTypeId(idlist);
        return AjaxResult.success(list);
    }



    /**
     * 将逗号分隔的字符串转换为Integer列表
     * @param taskTypeIds 逗号分隔的字符串，如"1,2,3,4"
     * @return 转换后的Integer列表，若输入为空则返回空列表
     */
    public List<Integer> convertToIntegerList(String taskTypeIds) {
        List<Integer> result = new ArrayList<>();
        if (taskTypeIds == null || taskTypeIds.trim().isEmpty()) {
            return result;
        }

        // 按逗号分割字符串
        String[] idArray = taskTypeIds.split(",");

        // 遍历数组并转换为Integer
        for (String idStr : idArray) {
            try {
                // 去除可能的空格并转换
                Integer id = Integer.parseInt(idStr.trim());
                result.add(id);
            } catch (NumberFormatException e) {
                // 处理转换失败的情况，这里可以根据实际需求选择忽略或抛出异常
                // 例如：log.error("转换ID失败: {}", idStr, e);
            }
        }

        return result;
    }

}
